﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ChartEdit
{
    public enum MTool
    {
        Pointer,
        Selection,
        Note,
        Tremolo,
        Arpeggiator,
        Pattern
    }
    public partial class MainForm : Form
    {
        public float Quantization = 1;
        private MTool tool = MTool.Pointer;
        public MTool Tool
        {
            get { return tool; }
            set
            {
                tool = value;
                foreach (EditForm edit in this.MdiChildren)
                {
                    edit.ed.MainTool = tool;
                }
            }
        }

        public MainForm()
        {
            InitializeComponent();

            toolStripQBox.SelectedIndex = 0;
        }

        public void SetStatus(string s)
        {
            toolStripStatusLabel.Text = "s";
        }

        private void openToolStripMenuItem_Click( object sender, EventArgs e )
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "Charts (*.chart)|*.chart|All Files (*.*)|*.*";
            ofd.Multiselect = true;
            ofd.Title = "Open Chart";

            if ( ofd.ShowDialog() != DialogResult.OK )
                return;

            foreach ( string fileName in ofd.FileNames )
            {
                addEditForm( fileName );
            }

            SetRenderingOptions();
        }

        private void addEditForm(string fileName)
        {
            EditForm ef = new EditForm(fileName,this);

            ef.MdiParent = this;
            ef.WindowState = FormWindowState.Normal;
            ef.Show();

        }

        private void cascadeToolStripMenuItem_Click( object sender, EventArgs e )
        {
            this.LayoutMdi( MdiLayout.Cascade );
        }

        private void tileVerticalToolStripMenuItem_Click( object sender, EventArgs e )
        {
            this.LayoutMdi( MdiLayout.TileVertical );
        }

        private void tileHorizontalToolStripMenuItem_Click( object sender, EventArgs e )
        {
            this.LayoutMdi( MdiLayout.TileHorizontal );
        }

        private void closeAllToolStripMenuItem_Click( object sender, EventArgs e )
        {
            foreach ( Form frm in OwnedForms )
            {
                frm.Close();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void highQualityToolStripMenuItem_Click(object sender, EventArgs e)
        {
            highQualityToolStripMenuItem.Checked = true;
            lowQualityToolStripMenuItem.Checked = false;
            SetRenderingQuality(RenderingMode.HighQuality);
        }

        private void lowQualityToolStripMenuItem_Click( object sender, EventArgs e )
        {
            lowQualityToolStripMenuItem.Checked = true;
            highQualityToolStripMenuItem.Checked = false;
            SetRenderingQuality(RenderingMode.LowQuality);
        }

        private void renderingModeToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        #region Draw "X" Boolean Checkbox Menu Things
        private void drawGEventsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetRenderingOptions();
        }

        private void drawTEventsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (EditForm edit in this.MdiChildren)
            {
                SetRenderingOptions();
            }
        }

        private void drawSyncToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (EditForm edit in this.MdiChildren)
            {
                SetRenderingOptions();
            }
        }

        private void drawSectionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (EditForm edit in this.MdiChildren)
            {
                SetRenderingOptions();
            }
        }

        private void drawNotesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (EditForm edit in this.MdiChildren)
            {
                SetRenderingOptions();
            }
        }

        private void SetRenderingOptions()
        {
            foreach (EditForm edit in this.MdiChildren)
            {
                edit.ed.R.DrawGlobalEvents = drawGEventsToolStripMenuItem.Checked;
                edit.ed.R.DrawTrackEvents = drawTEventsToolStripMenuItem.Checked;
                edit.ed.R.DrawSyncEvents = drawSyncToolStripMenuItem.Checked;
                edit.ed.R.DrawSections = drawSectionsToolStripMenuItem.Checked;
                edit.ed.R.DrawNotes = drawNotesToolStripMenuItem.Checked;
                edit.ed.Think();
            }
        }

        private void SetRenderingQuality(RenderingMode renderingMode)
        {
            foreach (EditForm edit in this.MdiChildren)
            {
                edit.ed.R.RenderingMode = renderingMode;
            }
        }
        #endregion

        private void MainForm_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = ( string[] )e.Data.GetData( DataFormats.FileDrop );

            foreach ( string fileName in files )
            {
                addEditForm( fileName );
            }

            SetRenderingOptions();
        }

        private void MainForm_DragEnter( object sender, DragEventArgs e )
        {
            if ( !e.Data.GetDataPresent( DataFormats.FileDrop, false ) )
                return;

            e.Effect = DragDropEffects.All;
        }

        private void toolStripOptions_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
        private int gcd(int a, int b)
        {
            int c;
            if (a < b)
            {
                c = a;
                a = b;
                b = c;
            }
            while (true)
            {
                c = a % b;
                if (c == 0)
                    return b;
                a = b;
                b = c;
            }
        }

        private void updateQuantization(string s)
        {
            if(!s.Contains('/'))
                return;
            int index = s.IndexOf('/');
            if (index==0 || s.Length - 1 == index)
                return;
            string nS = s.Substring(0, index);
            string dS = s.Substring(index+1);
            int n, d;
            try
            {
                n = Int32.Parse(nS);
                d = Int32.Parse(dS);
            }
            catch (System.FormatException)
            {
                return;
            }
            if (n <= 0 || d <= 0)
                return;
            if (n != 1)
            {
                int g = gcd(n, d);
                n = n / g;
                d = d / g;
            }
            Quantization = (float)n / d;

            foreach (EditForm ef in this.MdiChildren)
            {
                ef.ed.Quantization = (int)(this.Quantization * ef.ed.chart.Resolution * 4);
            }
        }

        private void toolStripQBox_TextUpdate(object sender, EventArgs e)
        {
            updateQuantization(toolStripQBox.Text);
        }
        private void resetToolbarColor()
        {
            foreach (ToolStripItem b in toolStrip.Items)
            {
                if (b is ToolStripButton)
                {
                    ToolStripButton a = (ToolStripButton)b;
                    a.BackColor = SystemColors.Control;
                    a.Checked = false;
                }
                    
            }
        }
        private void toolStripPointer_Click(object sender, EventArgs e)
        {
            resetToolbarColor();
            toolStripPointer.Checked = true;
            Tool = MTool.Pointer;
        }

        private void toolStripSelection_Click(object sender, EventArgs e)
        {
            resetToolbarColor();
            toolStripSelection.Checked = true;
            Tool = MTool.Selection;
        }

        private void toolStripNote_Click(object sender, EventArgs e)
        {
            resetToolbarColor();
            toolStripNote.Checked = true;
            Tool = MTool.Note;
        }

        private void toolStripTremolo_Click(object sender, EventArgs e)
        {
            resetToolbarColor();
            toolStripTremolo.Checked = true;
            Tool = MTool.Tremolo;
        }

        private void toolStripArpeggiator_Click(object sender, EventArgs e)
        {
            resetToolbarColor();
            toolStripArpeggiator.Checked = true;
            Tool = MTool.Arpeggiator;
        }

        private void toolStripPattern_Click(object sender, EventArgs e)
        {
            resetToolbarColor();
            toolStripPattern.Checked = true;
            Tool = MTool.Pattern;
        }

        private void saveToolStripMenuItem_Click( object sender, EventArgs e )
        {
            // DO NOTHING LOL
        }

        private void saveAsToolStripMenuItem_Click( object sender, EventArgs e )
        {
            EditForm ef = this.ActiveMdiChild as EditForm;

            if ( ef == null )
                return; // this should never really happen, all mdi children _should_ be EditForms

            ef.Save();
        }

 

    }
}
